<template>
    <div class="settings-container">
        <div class="settings-card">
            <div class="settings-header">
                <h1>游戏设置</h1>
            </div>

            <div class="settings-content">
                <div class="settings-section">
                    <h2>音频设置</h2>
                    <div class="setting-item">
                        <div class="setting-label">背景音乐</div>
                        <div class="setting-control">
                            <el-slider v-model="settings.music" :max="100" :min="0" />
                            <div class="setting-value">{{ settings.music }}%</div>
                        </div>
                    </div>
                    <div class="setting-item">
                        <div class="setting-label">音效</div>
                        <div class="setting-control">
                            <el-slider v-model="settings.sfx" :max="100" :min="0" />
                            <div class="setting-value">{{ settings.sfx }}%</div>
                        </div>
                    </div>
                </div>

                <div class="settings-section">
                    <h2>画面设置</h2>
                    <div class="setting-item">
                        <div class="setting-label">全屏模式</div>
                        <div class="setting-control switch">
                            <el-switch v-model="settings.fullscreen" />
                        </div>
                    </div>
                    <div class="setting-item">
                        <div class="setting-label">高画质</div>
                        <div class="setting-control switch">
                            <el-switch v-model="settings.highQuality" />
                        </div>
                    </div>
                </div>

                <div class="settings-section">
                    <h2>账号设置</h2>
                    <div class="setting-item">
                        <div class="setting-label">当前用户</div>
                        <div class="setting-value user-info">{{ userStore.username }}</div>
                    </div>
                    <div class="setting-item">
                        <button class="danger-btn" @click="logoutDialogVisible = true">
                            退出登录
                        </button>
                    </div>
                </div>
            </div>

            <div class="settings-footer">
                <button class="save-btn" @click="saveSettings">保存设置</button>
                <button class="reset-btn" @click="resetSettings">恢复默认</button>
                <button class="back-btn" @click="goBack">返回</button>
            </div>
        </div>
    </div>

    <!-- 退出登录确认对话框 -->
    <el-dialog
        v-model="logoutDialogVisible"
        :show-close="false"
        center
        class="logout-dialog"
        title="确认退出"
        width="400px"
    >
        <div class="dialog-content">确定要退出登录吗？</div>
        <template #footer>
            <div class="dialog-footer">
                <button class="dialog-btn cancel" @click="logoutDialogVisible = false">取消</button>
                <button class="dialog-btn confirm" @click="handleLogout">确认退出</button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
    import { onMounted, ref } from 'vue'
    import { useRouter } from 'vue-router'
    import { ElDialog, ElMessage, ElSlider, ElSwitch } from 'element-plus'
    import { useUserStore } from '@/stores/user'

    const router = useRouter()
    const userStore = useUserStore()

    // 游戏设置
    const settings = ref({
        music: 80,
        sfx: 70,
        fullscreen: true,
        highQuality: true,
    })

    // 退出确认对话框
    const logoutDialogVisible = ref(false)

    // 保存设置
    const saveSettings = () => {
        // 实际项目中可能需要保存到本地存储或服务器
        localStorage.setItem('game_settings', JSON.stringify(settings.value))
        ElMessage.success('设置已保存')
    }

    // 恢复默认设置
    const resetSettings = () => {
        settings.value = {
            music: 80,
            sfx: 70,
            fullscreen: true,
            highQuality: true,
        }
        ElMessage.success('已恢复默认设置')
    }

    // 退出登录
    const handleLogout = () => {
        userStore.logout()
        ElMessage.success('已成功退出登录')
        router.push('/login')
    }

    // 返回主菜单
    const goBack = () => {
        router.push('/')
    }

    // 加载保存的设置
    onMounted(() => {
        const savedSettings = localStorage.getItem('game_settings')
        if (savedSettings) {
            settings.value = JSON.parse(savedSettings)
        }
    })
</script>

<style scoped>
    .settings-container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        width: 100vw;
    }

    .settings-card {
        width: 600px;
        background-color: rgba(0, 0, 0, 0.7);
        border-radius: 12px;
        padding: 30px;
        box-shadow: 0 0 30px rgba(255, 215, 0, 0.3);
        border: 1px solid rgba(255, 215, 0, 0.3);
        color: #fff;
        animation: fadeIn 0.5s ease-in-out;
        transform: scale(1);
        transition: transform 0.3s ease;
    }

    .settings-card:hover {
        transform: scale(1.01);
        box-shadow: 0 0 40px rgba(255, 215, 0, 0.5);
    }

    .settings-header {
        text-align: center;
        margin-bottom: 30px;
        border-bottom: 1px solid rgba(255, 215, 0, 0.3);
        padding-bottom: 15px;
    }

    .settings-header h1 {
        font-size: 28px;
        color: #ffd700;
        text-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
        margin: 0;
        letter-spacing: 2px;
    }

    .settings-content {
        margin-bottom: 30px;
    }

    .settings-section {
        margin-bottom: 25px;
    }

    .settings-section h2 {
        font-size: 20px;
        color: #ffd700;
        margin-bottom: 15px;
        border-bottom: 1px solid rgba(255, 215, 0, 0.2);
        padding-bottom: 8px;
    }

    .setting-item {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
        padding: 5px 0;
    }

    .setting-label {
        width: 100px;
        font-size: 16px;
    }

    .setting-control {
        flex: 1;
        display: flex;
        align-items: center;
    }

    .setting-control.switch {
        justify-content: flex-start;
    }

    .setting-value {
        min-width: 50px;
        text-align: right;
        margin-left: 10px;
    }

    .user-info {
        font-weight: bold;
        color: #ffd700;
    }

    .settings-footer {
        display: flex;
        justify-content: space-between;
        margin-top: 20px;
        padding-top: 20px;
        border-top: 1px solid rgba(255, 215, 0, 0.3);
    }

    .save-btn,
    .reset-btn,
    .back-btn,
    .danger-btn {
        padding: 12px 20px;
        font-size: 16px;
        font-weight: bold;
        border-radius: 6px;
        cursor: pointer;
        transition: all 0.3s;
        border: none;
    }

    .save-btn {
        background-color: #ffd700;
        color: #000;
    }

    .save-btn:hover {
        background-color: #ffc000;
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(255, 215, 0, 0.4);
    }

    .reset-btn {
        background-color: transparent;
        color: #fff;
        border: 1px solid #fff;
    }

    .reset-btn:hover {
        background-color: rgba(255, 255, 255, 0.1);
        transform: translateY(-2px);
    }

    .back-btn {
        background-color: #666;
        color: #fff;
    }

    .back-btn:hover {
        background-color: #555;
        transform: translateY(-2px);
    }

    .danger-btn {
        background-color: #ff5757;
        color: white;
        padding: 8px 15px;
    }

    .danger-btn:hover {
        background-color: #ff3838;
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(255, 87, 87, 0.4);
    }

    /* 对话框样式 */
    :deep(.logout-dialog) {
        background-color: rgba(0, 0, 0, 0.8);
        border-radius: 10px;
        border: 1px solid rgba(255, 215, 0, 0.3);
    }

    .dialog-footer {
        display: flex;
        justify-content: center;
        gap: 20px;
        padding-top: 10px;
    }

    .dialog-btn {
        padding: 10px 20px;
        border-radius: 6px;
        font-size: 16px;
        font-weight: bold;
        cursor: pointer;
        transition: all 0.3s;
        border: none;
    }

    .dialog-btn.cancel {
        background-color: #666;
        color: white;
    }

    .dialog-btn.cancel:hover {
        background-color: #555;
    }

    .dialog-btn.confirm {
        background-color: #ff5757;
        color: white;
    }

    .dialog-btn.confirm:hover {
        background-color: #ff3838;
        box-shadow: 0 5px 15px rgba(255, 87, 87, 0.4);
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(-20px) scale(0.95);
        }
        to {
            opacity: 1;
            transform: translateY(0) scale(1);
        }
    }
</style>
